home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 318 / utilsrc / gprof.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  70.4 KB  |  2,402 lines

  1. /* `gprof', analyze gmon.out and print a profile.
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously
  32. and appropriately publish on each copy a valid copyright notice
  33. "Copyright (C) 1987 Free Software Foundation, Inc.", and include
  34. following the copyright notice a verbatim copy of the above disclaimer
  35. of warranty and of this License.
  36.  
  37.   2. You may modify your copy or copies of this source file or
  38. any portion of it, and copy and distribute such modifications under
  39. the terms of Paragraph 1 above, provided that you also do the following:
  40.  
  41.     a) cause the modified files to carry prominent notices stating
  42.     that you changed the files and the date of any change; and
  43.  
  44.     b) cause the whole of any work that you distribute or publish,
  45.     that in whole or in part contains or is a derivative of this
  46.     program or any part thereof, to be licensed at no charge to all
  47.     third parties on terms identical to those contained in this
  48.     License Agreement (except that you may choose to grant more
  49.     extensive warranty protection to third parties, at your option).
  50.  
  51.     c) You may charge a distribution fee for the physical act of
  52.     transferring a copy, and you may at your option offer warranty
  53.     protection in exchange for a fee.
  54.  
  55.   3. You may copy and distribute this program or any portion of it in
  56. compiled, executable or object code form under the terms of Paragraphs
  57. 1 and 2 above provided that you do the following:
  58.  
  59.     a) cause each such copy to be accompanied by the
  60.     corresponding machine-readable source code, which must
  61.     be distributed under the terms of Paragraphs 1 and 2 above; or,
  62.  
  63.     b) cause each such copy to be accompanied by a
  64.     written offer, with no time limit, to give any third party
  65.     free (except for a nominal shipping charge) a machine readable
  66.     copy of the corresponding source code, to be distributed
  67.     under the terms of Paragraphs 1 and 2 above; or,
  68.  
  69.     c) in the case of a recipient of this program in compiled, executable
  70.     or object code form (without the corresponding source code) you
  71.     shall cause copies you distribute to be accompanied by a copy
  72.     of the written offer of source code which you received along
  73.     with the copy you received.
  74.  
  75.   4. You may not copy, sublicense, distribute or transfer this program
  76. except as expressly provided under this License Agreement.  Any attempt
  77. otherwise to copy, sublicense, distribute or transfer this program is void and
  78. your rights to use the program under this License agreement shall be
  79. automatically terminated.  However, parties who have received computer
  80. software programs from you with this License Agreement will not have
  81. their licenses terminated so long as such parties remain in full compliance.
  82.  
  83.   5. If you wish to incorporate parts of this program into other free
  84. programs whose distribution conditions are different, write to the Free
  85. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  86. worked out a simple rule that can be stated here, but we will often permit
  87. this.  We will be guided by the two goals of preserving the free status of
  88. all derivatives of our free software and of promoting the sharing and reuse of
  89. software.
  90.  
  91.  In other words, you are welcome to use, share and improve this program.
  92.  You are forbidden to forbid anyone else to use, share and improve
  93.  what you give them.   Help stamp out software-hoarding!  */
  94.  
  95. /* GNU gprof was written mainly by Jay Fenlason.  */
  96.  
  97. #include "stddef.h"
  98. #include "stdarg.h"
  99. #undef NULL
  100. #include <stdio.h>
  101. #include <a.out.h>
  102. #include <assert.h>
  103. #include "gmon.h"
  104. /* #include <nlist.h> */
  105.  
  106. /* Names or default names for various files.  */
  107. #define OBJ_NAM "a.out"
  108. #define MON_NAM "gmon.out"
  109. #define SUM_NAM "gmon.sum"
  110.  
  111. /* Debugging stuff */
  112.  
  113. #define DEBUG
  114.  
  115. /* A mask of flags defined below.  */
  116. long debug=0;
  117.  
  118. /* Print obnoxious msgs about the a.out file, and the amusing values therein */
  119. #define DB_AFILE    (1<<0)    /* a.out file */
  120.  
  121. /* Describe in detail the rending and tearing of the gmon.out file */
  122. #define DB_GFILE    (1<<1)    /* gmon.out file */
  123.  
  124. /* Describe in detail the writing out of the gmon.sum file */
  125. #define DB_SUM        (1<<2)    /* gmon.sum file */
  126.  
  127. /* Print neeto messages as we deal with -e -E -f and -F options */
  128. #define DB_OPT        (1<<3)    /* Options */
  129.  
  130. /* Print msgs whenever the ring buffer is used */
  131. /* This probably doesn't work since the ring buffer code was
  132.    moved into the utilities file */
  133. #define DB_RB        (1<<4)    /* Ring buffer */
  134.  
  135. /* Print msgs about cycles */
  136. #define DB_CYC        (1<<5)
  137.  
  138. /* Print msgs about assigning the histogram entries to functions */
  139. #define DB_HISTO        (1<<6)
  140.  
  141. /* Print obnoxious msgs here and there as we do our stuff */
  142. #define DB_MISC        (1<<31)    /* misc stuff */
  143.  
  144. #ifdef DEBUG
  145. #define PRINT_OBNOXIOUS_DEBUG_MESSAGE(x,msg) if(debug&x) dbgprintf msg
  146. #else
  147. #define PRINT_OBNOXIOUS_DEBUG_MESSAGE(x,msg)
  148. #endif
  149.  
  150. /* LessThan EQual GreaterThan */
  151. /* These get returned by the various comparison functions */
  152. #define LT (-1)
  153. #define EQ (0)
  154. #define GT (1)
  155.  
  156. /* Note since *ALL* non-zero values are true, do *NOT* say
  157.       if(x==TRUE)
  158.    These values are here only for saying
  159.       return TRUE;
  160.    and things like that.   */
  161.  
  162. #define TRUE (1)
  163. #define FALSE (0)
  164.  
  165. /* The floating point datatype to use for storing
  166.    propagated info.  Float should be big enough */
  167. #define FLOAT    double
  168.  
  169. /* Used when creating a gmon.sum file */
  170. /* #define FUDGE_FACTOR (sizeof(CHUNK)) */
  171. #define FUDGE_FACTOR    0
  172.  
  173.  
  174. /* Description of one symbol in gprof's internal symbol table.
  175.    There is one of these for each function and one for each cycle.
  176.  
  177.    NAME is the name of the function or cycle.
  178.    VALUE is the address of the start of the function.
  179.  
  180.    CALLS and CALLED are chains of edges for calls into and out of
  181.    this function.  These constitute the call graph.
  182.  
  183.    NCALLS is the total number
  184.    of times this function called other functions,
  185.    NCALLED is the total number of times this function was called.
  186.    Note that if NCALLED is zero, but NCALLS is nozero, this function
  187.    must have been started magically.  It happens.  (Signals, etc.)
  188.  
  189.    RECURSIVE is the total number of times this function was called
  190.    recursively.
  191.  
  192.    HISTO is the total number of histogram counts for this function.
  193.  
  194.    SUB_HISTO is the total number of histogram counts for this function
  195.    plus time propagated from the children.
  196.  
  197.    CYCNUM is the number of the cycle this function is in,
  198.    or the number of this cycle is this entry is for a cycle.
  199.    -1 means the function isn't in any cycles.  0 means the
  200.    cycle-ness of the function hasn't been determined yet.
  201.  
  202.    NUMINDEX is the index number assigned to this function
  203.    for the call graph.
  204.  
  205.    If FLAG is non-zero, this function is in the process of being
  206.    saved from the oblivion caused by the -f or -F options.  */
  207.  
  208. struct mesym {
  209.     char *name;
  210.     unsigned long value;
  211.  
  212.     struct symlist *calls;
  213.     unsigned ncalls;
  214.     struct symlist *called;
  215.     unsigned ncalled;
  216.  
  217.     unsigned recursive;
  218.  
  219.     unsigned histo;
  220.     FLOAT sub_histo;
  221.     int    cycnum;
  222.     int    numindex;
  223.     int    flag;
  224. };
  225.  
  226. /* Vector of `struct mesym' for all functions,
  227.    sorted in ascending order by VALUE field for binary search.  */
  228. struct mesym *syms;
  229. int nsym=0;            /* Length of vector */
  230.  
  231. /* Vector of `struct mesym' for all cycles so far identified.  */
  232. struct mesym **cycles;
  233. int number_of_cycles = 0;        /* Length of vector */
  234.  
  235. /* Structure for an edges of the call graph.
  236.  
  237.    Each edge--each pair of functions A and B such that A called B--
  238.    is represented by one of these structures.  It appears in
  239.    A's `calls' chain and in B's `called' chain.
  240.  
  241.    Meanwhile, the structure describes its meaning with the
  242.    SYM_FROM and SYM_TO fields, which point to the symbol entries
  243.    for A and B.
  244.  
  245.    Basically, if you got here from a mesym's calls
  246.    pointer, SYM_FROM points back to that symbol, NEXT_FROM is irrevelant,
  247.    SYM_TO points to the symbol it called, and NEXT_TO points to the
  248.    next one in the list.
  249.  
  250.    If you got here from a mesym's called pointer, SYM_FROM points to
  251.    the symbol that called it, NEXT_FROM points to the next one in the
  252.    list, SYM_TO points back to the symbol, and NEXT_TO is irrelevant.
  253.  
  254.    NCALLS is the number of times SYM_FROM called SYM_TO,
  255.    PROP_TIME is the amount of time in SYM_TO itself propagated to SYM_FROM,
  256.    CHILD_TIME is the amount of time
  257.      propagated from SYM_TO's children to SYM_FROM.  */
  258.    
  259. struct symlist {
  260.     struct mesym *sym_from;
  261.     struct symlist *next_from;
  262.     struct mesym *sym_to;
  263.     struct symlist *next_to;
  264.  
  265.     unsigned ncalls;
  266.     FLOAT    prop_time;
  267.     FLOAT    child_time;
  268. };
  269.  
  270. /* argv[0], here for the sake of error messages.  */
  271. char *myname = 0;
  272.  
  273. /* Header of the executable file.  */
  274. struct exec exec_header;
  275.  
  276. /* Name of the executable file.  */
  277. char *exec_file_name;
  278.  
  279. /* The string table of the executable file.
  280.    Each symbol entry in the file contains an index in the string table.
  281.    When the symbols are in core, we relocate them to point to their names,
  282.    which remain inside the string table.  */
  283. char *strs;
  284.  
  285. /* Header of the first gmon.out file we read.  This is used to (try to)
  286.    make sure all the rest of the gmon.out files agree with the first one
  287.    (and the executable.)  */
  288. struct gm_header hdr;
  289.  
  290. /* Number of clock ticks per second.  Read from the kernel's memory,
  291.    this number tells us how long an interval a single stab in the
  292.    histogram represents. */
  293. long ticks;
  294.  
  295. /* This is an array of pointers to symbols.  These are the functions that
  296.    we'll have to print out in the flat graph.  */
  297. struct mesym **flat_profile_functions;
  298. int number_of_flat_profile_functions =  0; /* Size of vector */
  299.  
  300. /* This is an array of pointers to symbols.  These are the functions that
  301.    we should mention in the call tree.  */
  302. struct mesym **functions_in_call_tree;
  303. int number_of_functions_in_call_tree= 0;
  304. struct mesym **f_end;
  305.  
  306. /* Number of slots in the histogram.  */
  307. int nhist;
  308.  
  309. /* Total number of counts in the histogram.  */
  310. unsigned long tothist = 0;
  311.  
  312. /* Pointer to the histogram itself.  */
  313. unsigned CHUNK *histo;    /* Stabs */
  314.  
  315. /* Record the specified options.  */
  316.  
  317. /* If the -a option is given, static (private) functions are not read into
  318.    the symbol table.  This means that time spent in them, calls to them, etc,
  319.    are instead added to whatever function was loaded next to it in the a.out
  320.    file.  This is compatable with UN*X gprof.  (Bleh.)  The right thing to do
  321.    is keep track of time spent in static functions, and forget about it,
  322.    instead of adding it to another hapless function.  Rms disagrees with me.
  323.    I think its evil to add histogram time to a function simply because it
  324.    happend to be loaded in memory just before a function we don't want to print. */
  325. int no_locals = 0;        /* -a flag */
  326.  
  327. /* The -b option tells gprof to not print out the obnoxious blurbs telling
  328.    what all the fields of the output mean.  This is useful if you've seen
  329.    the blurbs a gzillion times and you only want to look at your numbers. */
  330. int no_blurbs = 0;        /* -b flag */
  331.  
  332. /* If the -s option is given, gprof will write out a 'sum file' gmon.sum
  333.    which is a gmon.out file that contains the profile info from all the
  334.    gmon.out files that gprof read in. */
  335. int write_sum_file = 0;        /* -s option */
  336.  
  337. /* The -z option tells gprof to include functions with zero usage (never called,
  338.    and used no time) in the output.  Usually, such functions are considered
  339.    boring, and aren't printed. */
  340. int print_zeros = 0;        /* -z option */
  341.  
  342. /* The -e option takes a function name, and suppress the printing of that
  343.    function and its descendents from the call graph profile.  (If its
  344.    descendents are called from elswhere, well. . .)  (Currently, they're
  345.    printed, and the -e'd function is shown under the list of parents so
  346.    you can see where the child's time is disappearing to. . .)
  347.  
  348.    The -E option takes a function name, and not only -e's it, but
  349.    removes the time spent in the function from the total time.  (Its as if
  350.    that function never existed (unless it calls something that is also
  351.    called from somewhere else, in which case. . .  (Currently it works
  352.    as described for -e above.))
  353.  
  354.    -f prints only the call tree for its argument.
  355.  
  356.    -F is like -f, but it uses only the time in the function and its
  357.    descendents for computing total time.
  358.  */
  359.  
  360. enum option_type { SMALL_E, BIG_E, SMALL_F, BIG_F };
  361.  
  362. struct filter {
  363.     char *name;
  364.     enum option_type type;
  365. };
  366.  
  367. /* Vector with an element for each -e, -E, -f or -F option specified.  */
  368. struct filter *filters;
  369. /* Length of the vector.  */
  370. int nfilters;
  371.  
  372. /* These values are stored in the flag field of a struct mesym so we can know
  373.    what we're doing to that function */
  374.  
  375. /* This one is being saved by a -f or -F flag */
  376. #define SAVE_ME        01
  377. /* This one is being killed by a -e or -E flag */
  378. #define KILL_ME        02
  379.  
  380. /* Blurbs that are copied verbatim into the output file
  381.    to explain the data in the tables.
  382.  
  383.    If your compiler can't stand this, split this up into a vector
  384.    of strings, and print them one after another. */
  385.  
  386. char *first_blurb = "\n\
  387.  The above table shows how much time was spent directly in each\n\
  388.  function.  The table was sorted by the amount of time the computer\n\
  389.  spent in each function.\n\n\
  390.  % time        This is the percentage of the total execution time\n\
  391.         the program spent in this function.  These should all add\n\
  392.         up to 100%.\n\n\
  393.  seconds       This is the total number of seconds the computer spent\n\
  394.         executing this function.\n\n\
  395.  cumsec        This is the cumulative total number of seconds the\n\
  396.         computer spent executing this functions, plus the time spent\n\
  397.      in all the functions above this one in this table.\n\n\
  398.  calls         This is the total number of times the function was called.\n\
  399.         If there isn't a number here, the function wasn't compiled with\n\
  400.     the profiler enabled, and further information about this function\n\
  401.     is limited.  In particular, all information about where this \n\
  402.     function was called from has been lost.\n\n\
  403.  function      This is the name of the function.\n\
  404. \f";
  405.  
  406. char *second_blurb = "\n\
  407.  This table describes the call tree of the program, and was sorted by\n\
  408.  the total amount of time spent in each function and its children.\n\n\
  409.  Each entry in this table consists of several lines.  The line with the\n\
  410.  index number at the left hand margin lists the current function.\n\
  411.  The lines above it list the functions that called this function,\n\
  412.  and the lines below it list the functions this one called.\n\
  413.  This line lists:\n\
  414.      index    A unique number given to each element of the table.\n\
  415.         Index numbers are sorted numerically.\n\
  416.         The index number is printed next to every function name so\n\
  417.         it is easier to look up where the function in the table.\n\n\
  418.      % time    This is the percentage of the `total' time that was spent\n\
  419.         in this function and its children.  Note that due to\n\
  420.         different viewpoints, functions excluded by options, etc,\n\
  421.         these numbers will NOT add up to 100%.\n\n\
  422.      self    This is the total amount of time spent in this function.\n\n\
  423.      children    This is the total amount of time propagated into this\n\
  424.         function by its children.\n\n\
  425.      called    This is the number of times the function was called.\n\
  426.         If the function called itself recursively, the number\n\
  427.         only includes non-recursive calls, and is followed by\n\
  428.         a `+' and the number of recursive calls.\n\n\
  429.      name    The name of the current function.  The index number is\n\
  430.         printed after it.  If the function is a member of a\n\
  431.         cycle, the cycle number is printed between the\n\
  432.         function's name and the index number.\n\n\n\
  433.  For the function's parents, the fields have the following meanings:\n\n\
  434.      self    This is the amount of time that was propagated directly\n\
  435.         from the function into this parent.\n\n\
  436.      children    This is the amount of time that was propagated from\n\
  437.         the function's children into this parent.\n\n\
  438.      called    This is the number of times this parent called the\n\
  439.         function `/' the total number of times the function\n\
  440.         was called.  Recursive calls to the function are not\n\
  441.         included in the number after the `/'.\n\n\
  442.      name    This is the name of the parent.  The parent's index\n\
  443.         number is printed after it.  If the parent is a\n\
  444.         member of a cycle, the cycle number is printed between\n\
  445.         the name and the index number.\n\n\
  446.  If the parents of the function cannot be determined, the word\n\
  447.  `<spontaneous>' is printed in the `name' field, and all the other\n\
  448.  fields are blank.\n\n\
  449.  For the function's children, the fields have the following meanings:\n\n\
  450.      self    This is the amount of time that was propagated directly\n\
  451.         from the child into the function.\n\n\
  452.      children    This is the amount of time that was propagated from the\n\
  453.         child's children to the function.\n\n\
  454.      called    This is the number of times the function called\n\
  455.         this child `/' the total number of times the child\n\
  456.         was called.  Recursive calls by the child are not\n\
  457.         listed in the number after the `/'.\n\n\
  458.      name    This is the name of the child.  The child's index\n\
  459.         number is printed after it.  If the child is a\n\
  460.         member of a cycle, the cycle number is printed\n\
  461.         between the name and the index number.\n\n\
  462.  If there are any cycles (circles) in the call graph, there is an\n\
  463.  entry for the cycle-as-a-whole.  This entry shows who called the\n\
  464.  cycle (as parents) and the members of the cycle (as children.)\n\
  465.  The `+' recursive calls entry shows the number of function calls that\n\
  466.  were internal to the cycle, and the calls entry for each member shows,\n\
  467.  for that member, how many times it was called from other members of\n\
  468.  the cycle.\n\n";
  469.  
  470. /* Prototypes for all the functions.  */
  471.  
  472. /* Since I don't think its in the library yet. */
  473. int getopt(int,char **,char *);
  474.  
  475. int    main(int,char **);
  476. void    print_flat_profile(void);
  477. void    print_call_graph(void);
  478. void    write_summary(void);
  479. void    filter_graph(void);
  480. FLOAT    convert_and_round(FLOAT);
  481. void    add_to_lists(struct mesym *,struct mesym *,unsigned);
  482. void    delete_from_lists(struct mesym*,struct mesym *);
  483. void    flushfuns(void);
  484. void    findcycles(void);
  485. void    kill_children(struct mesym *,int);
  486. void    save_the_children(struct mesym *,int);
  487. void    remove_from_call_tree(struct mesym **);
  488. struct mesym **find_funp_from_name(char *);
  489. struct mesym **find_funp_from_pointer(struct mesym *);
  490. void    read_syms(FILE *,int);
  491. struct mesym *val_to_sym(unsigned long);
  492. int    badsym(struct nlist *);
  493. int    symcmp(void *,void *);
  494. int    timecmp(void *,void *);
  495. int    callcmp(void *,void *);
  496. int    treetimecmp(void *,void *);
  497. int    listcmp(void *,void *);
  498. void    readgm(char *);
  499. void    print_blurb(char *blurb);
  500. long    get_ticks(void);
  501. void    add_filter(char *name,int flag);
  502. void    print_sorted_list(int,int,struct symlist *);
  503.  
  504. void    fatal(char *printf_string,...);
  505. void    fatal_io(char *problem,char *filename);
  506.  
  507. FILE *ck_fopen(char *name,char *mode);
  508. void ck_fseek(FILE *fp,long i,int w);
  509. void ck_fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
  510. void ck_fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream);
  511. void ck_fclose(FILE *stream);
  512.  
  513. void *ck_malloc(size_t size);
  514. void *ck_calloc(size_t nmemb,size_t size);
  515. void *ck_realloc(void *,size_t);
  516.  
  517. char *mk_sprintf(char *printf_string,...);
  518.  
  519. void init_ring_buffer(void);
  520. void push_ring_buffer(void *what_to_push);
  521. void *pop_ring_buffer(void);
  522. int ring_buffer_isnt_empty(void);
  523.  
  524. /* Since we don't have prototypes for the system funs, we add them
  525.    ourselves. . . */
  526. int    atoi(const char *);
  527. long    atol(const char *);
  528. double    atof(const char *);
  529.  
  530. void    qsort(void *,int,int,int (*)());
  531.  
  532. void    free(void *);
  533.  
  534. void    exit(int);
  535.  
  536. int    strcmp(const char *,const char *);
  537. char    *index(const char *,int);
  538. int    printf(const char *,...);
  539. int    fprintf(FILE *,const char *,...);
  540. /* char    *sprintf(const char *,const char *,...); */
  541.  
  542. int    puts(const char *);
  543. int    fputs(const char *,FILE *);
  544.  
  545. int    fputc(int,FILE *);
  546. int    fread(void *,int,int,FILE *);
  547.  
  548. int    nlist(const char *,struct nlist *);
  549.  
  550. int    vfprintf(FILE *,const char *,va_list);
  551.  
  552. void dbgprintf(char *s,...);
  553. void dumpsyms(void);
  554. void dumpfuns(void);
  555.  
  556. /* And now, the program.  */
  557.  
  558. int
  559. main(int ac,char **av)
  560. {
  561.   FILE    *fp;
  562.   int    argc;
  563.   char    **argv;
  564.   int    c;
  565.   int n;
  566.  
  567.   extern char *optarg;
  568.   extern int optind,opterr;
  569.  
  570.   argc=ac;
  571.   argv=av;
  572.  
  573.   myname= argv[0];
  574.  
  575.   /* Omit profiling internal functions from call graph.  */
  576.   add_filter("mcount",BIG_E);
  577.   /* add_filter("mcleanup",BIG_E); Seems to have dissappeared? */
  578.   add_filter("moncontrol",BIG_E);
  579.  
  580.   init_ring_buffer();
  581.  
  582.   while((c=getopt(argc,argv,"abcdD:e:E:f:F:svz"))!=EOF) {
  583.     switch(c) {
  584.     case 'a':
  585.       no_locals= TRUE;
  586.       break;
  587.     case 'b':
  588.       no_blurbs = TRUE;
  589.       break;
  590.     case 'c':
  591.       fatal("The -c option is not supported");
  592.     case 'd':
  593.       debug= -1;
  594.       break;
  595.     case 'D':
  596.       debug=atoi(optarg);
  597.       break;
  598.     case 's':
  599.       write_sum_file= TRUE;
  600.       break;
  601.     case 'z':
  602.       print_zeros = TRUE;
  603.       break;
  604.     case 'e':
  605.       add_filter(optarg,SMALL_E);
  606.       break;
  607.     case 'E':
  608.       add_filter(optarg,BIG_E);
  609.       break;
  610.     case 'f':
  611.       add_filter(optarg,SMALL_F);
  612.       break;
  613.     case 'F':
  614.       add_filter(optarg,BIG_F);
  615.       break;
  616.     default:
  617.       fprintf(stderr,
  618.           "Usage:  %s -absz -e func -E func -f func -F func executable gmon.out ...\n",
  619.           myname);
  620.       break;
  621.     }
  622.   }
  623.   if(optind<argc) {
  624.     exec_file_name=argv[optind];
  625.     optind++;
  626.   }
  627.   if(!exec_file_name)
  628.     exec_file_name=OBJ_NAM;
  629.  
  630.   /* Open the a.out file, and read in selected portions */
  631.   fp=ck_fopen(exec_file_name,"r");
  632.   ck_fread((void *)&exec_header,sizeof(exec_header),1,fp);
  633.  
  634.   /* Make sure its really an a.out file.  If it isn't yell and scream
  635.      and stamp our feet. */
  636.   if(N_BADMAG(exec_header))
  637.     fatal("`%s' is not an executable file", exec_file_name);
  638.  
  639.   /* Read in the string table.  */
  640.   ck_fseek(fp,N_STROFF(exec_header),0);
  641.   ck_fread((void *)&n,sizeof(n),1,fp);
  642.   strs=(char *)ck_malloc(n);
  643.   ck_fread((void *)(strs+sizeof(long)),sizeof(char),n-sizeof(long),fp);
  644.  
  645.   /* Read the symbols, and put the interesting ones (sorted) in SYMS.  */
  646.   ck_fseek(fp,N_SYMOFF(exec_header),0);
  647.   read_syms(fp,exec_header.a_syms/sizeof(struct nlist));
  648.  
  649.   ck_fclose(fp);
  650.   /* We are done with the a.out file */
  651.  
  652.   /* Read in the gmon.out files; accumulate all histogram data in HISTO
  653.      and put all number-of-calls figures into the call graph.  */
  654.   if(optind>=argc) readgm(MON_NAM);
  655.   else {
  656.     while(optind<argc) {
  657.       readgm(argv[optind]);
  658.       optind++;
  659.     }
  660.   }
  661.  
  662.   /* If a summary output file is wanted,
  663.      we can do it straightaway since we have merged the data.  */
  664.  
  665.   if(write_sum_file) {
  666.     write_summary ();
  667.     exit (0);
  668.   }
  669.  
  670.   /* Find out how many clock ticks/second our machine puts out */
  671.   ticks=get_ticks();
  672.  
  673.   /* Assign the ticks in the histogram to the functions they represent */
  674.   /* Blind faith assures us that no histogram entry refers to
  675.      more than one symbol. */
  676.   {
  677.     int    n;
  678.     unsigned long pos;
  679.     struct mesym *sym;
  680.     int incr = (hdr.high - hdr.low) / nhist;
  681.  
  682.     if ((hdr.high - hdr.low) != 4 * nhist)
  683.       abort ();
  684.  
  685.     for(n=0,pos=hdr.low,sym= &syms[0];n<nhist;n++,pos+=incr) {
  686.       if(histo[n]) {
  687.  
  688.     /* We've found the right symbol when *sym<=pos && *(sym+1)>pos */
  689.     /* This means POS lies between sym and the one after it */
  690.  
  691.     while((sym+1)->value<=pos)
  692.       sym++;
  693.     if((sym+1)->value<(pos+incr)) {
  694.         sym->histo+=(histo[n]+1)/2;
  695.         (sym+1)->histo+=(histo[n])/2;
  696.         if(debug&DB_HISTO)
  697.             fprintf(stderr,"%05lx %02d-> %s (%d) %s (%d)\n",pos,histo[n],sym->name,sym->histo,(sym+1)->name,(sym+1)->histo);
  698.     } else {
  699.         sym->histo+=histo[n];
  700.         if(debug&DB_HISTO)
  701.             fprintf(stderr,"%05lx %02d-> %s (%d)\n",pos,histo[n],sym->name,sym->histo);
  702.     }
  703.       }
  704.     }
  705.   }
  706.  
  707.   /* Avoid division by zero if there wasn't any time collected!  */
  708.   if(tothist==0)
  709.     tothist=1;
  710.  
  711.   print_flat_profile ();
  712.  
  713.   print_call_graph ();
  714.  
  715.   if(debug&DB_AFILE)
  716.     dumpsyms();
  717.  
  718.   if(debug&DB_AFILE)
  719.     dumpfuns();
  720. }
  721.  
  722. /* Output a summary gmon file containing all our accumulated
  723.    histogram and call-graph data.  */
  724.  
  725. void
  726. write_summary ()
  727. {
  728.   struct mesym *p;
  729.   struct symlist *t;
  730.   struct gm_call call_tmp;
  731.   FILE *fp;
  732.  
  733.   fp=ck_fopen(SUM_NAM,"w");
  734.   hdr.nbytes=sizeof(struct gm_header)+nhist*sizeof(CHUNK);
  735.   ck_fwrite((void *)&hdr,sizeof(hdr),1,fp);
  736.   ck_fwrite((void *)histo,sizeof(unsigned CHUNK),nhist,fp);
  737.   /* for(p= &syms[nsym];--p>=&syms[0];) { */
  738.   if(nsym) {
  739.     p= &syms[nsym-1];
  740.     do {
  741.       for(t=p->calls;t;t=t->next_to) {
  742.     /* Since we've forgotten exactly where in FROM we
  743.        were called from, we fake it.  Since this is only
  744.        gonna be fed back into gprof, it doesn't matter */
  745.     call_tmp.from=(p->value+FUDGE_FACTOR)-hdr.low;
  746.     call_tmp.to=(t->sym_to->value+FUDGE_FACTOR)-hdr.low;
  747.     call_tmp.ncalls=t->ncalls;
  748.     ck_fwrite((void *)&call_tmp,sizeof(call_tmp),1,fp);
  749.       }
  750.     } while(p-->&syms[0]);
  751.   }
  752.   ck_fclose(fp);
  753. }
  754.  
  755. /* Print the flat profile from the symbol table information.  */
  756.  
  757. void
  758. print_flat_profile ()
  759. {
  760.   int n;
  761.   struct mesym **funp;
  762.  
  763.   /* Scan the symbol table and discover how many functions either had time
  764.      spent in them, or had a non-zero call count */
  765.   for(n=0;n<nsym;n++) {
  766.     if(syms[n].histo || syms[n].ncalled || print_zeros)
  767.       number_of_flat_profile_functions++;
  768.   }
  769.   /* Collect all the interesting functions */
  770.   flat_profile_functions
  771.     =(struct mesym **)ck_calloc(number_of_flat_profile_functions,sizeof(struct mesym *));
  772.   for(n=0,funp=flat_profile_functions;n<nsym;n++) {
  773.     if(syms[n].histo || syms[n].ncalled || print_zeros) {
  774.       *funp= &syms[n];
  775.       funp++;
  776.     }
  777.   }
  778.  
  779.   /* Sort them */
  780.   qsort(flat_profile_functions,number_of_flat_profile_functions,
  781.     sizeof(struct mesym *),timecmp);
  782.  
  783.   /* And print them out */
  784.  
  785.   if (no_blurbs)
  786.     printf("\t\tFlat profile\n\n");
  787.   else
  788.     printf("\tFlat profile (explanation follows)\n\n");
  789.  
  790.   if (no_blurbs)
  791.     printf("\t\tCall graph is on the following page.\n\n");
  792.   else
  793.     printf("\tCall graph is on the following page.\n\n");
  794.  
  795.   printf("Each sample counts as %g seconds.\n\n",1.0/ticks);
  796.  
  797.   puts("% time  seconds   cumsec   calls  function");
  798.  
  799.   for(n=0;n<number_of_flat_profile_functions;n++) {
  800.     unsigned histo;
  801.     static cumhist = 0;
  802.  
  803.     histo=flat_profile_functions[n]->histo;
  804.     cumhist+=histo;
  805.     printf("%6.2f ", (FLOAT)(100.0*histo)/(FLOAT)tothist);
  806.     printf("%8.4f ", ((FLOAT)histo)/(FLOAT)ticks);
  807.     printf("%8.4f ", ((FLOAT)cumhist)/(FLOAT)ticks);
  808.     if(flat_profile_functions[n]->ncalled)
  809.       printf("  %5d  ", flat_profile_functions[n]->ncalled);
  810.     else fputs("         ",stdout);
  811.     printf("%s\n", flat_profile_functions[n]->name);
  812.   }
  813.  
  814.   /* RMS says we should print the blurb last, which makes sense to me */
  815.   print_blurb(first_blurb);
  816. }
  817.  
  818. /* Compute the call graph and print it.  */
  819.  
  820. void
  821. print_call_graph ()
  822. {
  823.   int n;
  824.   int index;
  825.   struct mesym **funp;
  826.   struct mesym **f;
  827.  
  828.   /* Count the functions that appear in the call tree.  */
  829.   for(n=0;n<nsym;n++) {
  830.     /* If a function calls something else, or is called by
  831.        something else, its in the call tree. . . */
  832.     if(syms[n].ncalls || syms[n].ncalled)
  833.       number_of_functions_in_call_tree++;
  834.   }
  835.  
  836.   /* Allocate a vector of thuese functions.  */
  837.   functions_in_call_tree
  838.     =(struct mesym **)ck_calloc(number_of_functions_in_call_tree,sizeof(struct mesym *));
  839.   for(n=0,funp=functions_in_call_tree;n<nsym;n++) {
  840.     if(syms[n].ncalls || syms[n].ncalled) {
  841.       *funp= &syms[n];
  842.       funp++;
  843.     }
  844.   }
  845.  
  846.   /* Put all the leaf nodes at the end of the call tree */
  847.   qsort(functions_in_call_tree,number_of_functions_in_call_tree,sizeof(struct mesym *),callcmp);
  848.  
  849.   /* Root nodes are now all at the head, and can be easily found
  850.      'cuz they have call-counts of zero (Never been called, but
  851.      calls something else; that's spontaneous in my book.) */
  852.   /* Ordinary nodes are in the middle, (were called, and
  853.      called others.  Leaf nodes are at the end. */
  854.  
  855.   /* Our mission, should we choose to accept it, is to detect
  856.      circles in the call graph. */
  857.   /* We do this by keeping a pointer into the call tree called f_end.  This
  858.      points to the end of the functions that we don't know if they are leaf
  859.      nodes or not.  When we know something is a leaf node, we move it down
  860.      past f_end */
  861.  
  862.   f= &functions_in_call_tree[number_of_functions_in_call_tree-1];
  863.   do {
  864.     if((*f)->ncalls!=0)
  865.       break;
  866.     (*f)->cycnum= -1;
  867.  
  868.     /* Note the neeto side effect here!  Is there a better way to do this? */
  869.   } while(f-- > &functions_in_call_tree[0]);
  870.  
  871.   /* Note that functions that only call themselves (and nobody else) don't
  872.      get marked above.  Doesn't matter; they get marked soon. */
  873.   if(f== &functions_in_call_tree[0]) {
  874.     (*f)->cycnum= -1;
  875.   } else {
  876.     int found;
  877.  
  878.     f_end=f;
  879.  
  880.     /* Eliminate recursive calls */
  881.     do {
  882.       struct symlist *t,*u;
  883.  
  884.       for(t= (*f)->calls;t;t=t->next_to) {
  885.     if(t->sym_to!= (*f))
  886.       continue;
  887.     (*f)->recursive+=t->ncalls;
  888.  
  889.     (*f)->ncalls-=t->ncalls;
  890.     (*f)->ncalled-=t->ncalls;
  891.  
  892.     /* Delete from linked list */
  893.     if(t==(*f)->calls)
  894.       (*f)->calls=t->next_to;
  895.     else {
  896.       for(u=(*f)->calls;u->next_to!=t;u=u->next_to)
  897.         ;
  898.       u->next_to=t->next_to;
  899.     }
  900.  
  901.     /* Find and delete from called list too */
  902.     if(t==(*f)->called)
  903.       (*f)->called=t->next_from;
  904.     else {
  905.       for(u=(*f)->called;u->next_from!=t;u=u->next_from)
  906.         ;
  907.       u->next_from=t->next_from;
  908.     }
  909.     free(t);
  910.     break;
  911.       }
  912.     } while(f--!=&functions_in_call_tree[0]);
  913.  
  914.     number_of_cycles = 0;
  915.  
  916.     /* Either there weren't any cycles, or all the cycles live
  917.        between f_end and functions_in_call_tree */
  918.  
  919.     /* Mark all functions that are not in any cycle.  */
  920.     flushfuns();
  921.  
  922.  
  923.     /* If we haven't got them all, find the cycle(s).  */
  924.     if (f_end != &functions_in_call_tree[0])
  925.       findcycles();
  926.   }
  927.  
  928.   /* Add entries for the cycles to the vector of all call-graph nodes.  */
  929.  
  930.   functions_in_call_tree
  931.     =ck_realloc(functions_in_call_tree,
  932.         (number_of_cycles+number_of_functions_in_call_tree)*sizeof(struct mesym *));
  933.   for(n=0;n<number_of_cycles;n++)
  934.     functions_in_call_tree[number_of_functions_in_call_tree++]= cycles[n];
  935.  
  936.   /* Now discard the functions that the filters say should not appear.  */
  937.  
  938.   filter_graph ();
  939.  
  940.   /* So by now, the only functions left are the ones we want to print */
  941.   qsort(functions_in_call_tree,number_of_functions_in_call_tree,sizeof(struct mesym *),treetimecmp);
  942.  
  943.   /* Assign each function its index number.  */
  944.   for(n=0;n<number_of_functions_in_call_tree;n++)
  945.     functions_in_call_tree[n]->numindex=n+1;
  946.  
  947.   if (no_blurbs)
  948.     printf("\t\t\tCall graph\n\n");
  949.   else
  950.     printf("\t\t     Call graph (explanation follows)\n\n");
  951.  
  952.   puts("index  % time    self  children called     name");
  953.  
  954.   /* Loop over entries.  */
  955.  
  956.   for(index = 0; index <number_of_functions_in_call_tree; index++) {
  957.     struct mesym *current = functions_in_call_tree[index];
  958.     char    tmpstr[40];    /* Can an int have more than 40 digits?  I hope not */
  959.  
  960.     /* Print out all the things that called this */
  961.     if(current->ncalled==0 && current->called==0)
  962.       printf("                                             <spontaneous>\n");
  963.     else {
  964.       print_sorted_list(current->ncalled,current->cycnum,current->called);
  965.     }
  966.         
  967.         
  968.     sprintf(tmpstr,"[%d]",current->numindex);
  969.     printf("%-6s %6.2f %7.2f  %7.2f ",
  970.        tmpstr,
  971.        (FLOAT)(100.0*(current->sub_histo+current->histo))/(FLOAT)tothist,
  972.        convert_and_round((FLOAT)current->histo),
  973.        convert_and_round((FLOAT)current->sub_histo));
  974.  
  975.     if(current->recursive) {
  976.       printf("%4d+%-4d %s",
  977.          current->ncalled,
  978.          current->recursive,
  979.          current->name);
  980.     } else
  981.       printf("%4d      %s",current->ncalled,current->name);
  982.  
  983.     if(current->cycnum>0 && current->name[0]!='<')
  984.       printf(" <cycle %d>",current->cycnum);
  985.  
  986.     printf(" [%d]\n",current->numindex);
  987.  
  988.  
  989.     /* Now print out the children */
  990.  
  991.     if(current->name[0]=='<')
  992.       print_sorted_list(-2,current->cycnum,current->calls);
  993.     else
  994.       print_sorted_list(-1,current->cycnum,current->calls);
  995.  
  996.  
  997.     printf("----------------------------------------\n");
  998.   }
  999.  
  1000.   print_blurb(second_blurb);
  1001. }
  1002.  
  1003. /* Scan the call tree for virtual leaf nodes */
  1004. /* If we find any, propagate time into them from their children,
  1005.    mark them as being leaf nodes.  Then repeat the process.  When
  1006.    we drop out of here, either we've flushed the entire tree, or
  1007.    we've found a cycle. */
  1008.  
  1009. void
  1010. flushfuns(void)
  1011. {
  1012.   int    found;
  1013.   struct mesym **f;
  1014.  
  1015.   do {
  1016.     found=0;
  1017.     f=f_end;
  1018.     do {
  1019.       struct symlist *t;
  1020.  
  1021.       assert(f>=functions_in_call_tree);
  1022.  
  1023.       for(t=(*f)->calls;t;t=t->next_to)
  1024.     if(t->sym_to->cycnum==0)
  1025.       break;
  1026.  
  1027.       /* We've found an virtual leaf node.  We shold
  1028.      propagate time into the node, and move it
  1029.      past f_end, since we aren't interested in
  1030.      it anymore */
  1031.       if(!t) {
  1032.     found++;
  1033.  
  1034.     /* If its a member of a cycle, cycnum is positive, and
  1035.        time propagation has already been dealt with. */
  1036.     if((*f)->cycnum==0) {
  1037.       for(t=(*f)->calls;t;t=t->next_to) {
  1038.         struct mesym *symP;
  1039.  
  1040.         if(t->sym_to->name[0]=='<')
  1041.           continue;
  1042.         if(t->sym_to->cycnum==-1)
  1043.           symP= t->sym_to;
  1044.         else 
  1045.           symP= cycles[t->sym_to->cycnum-1];
  1046.  
  1047.         t->prop_time= (FLOAT)t->ncalls*(FLOAT)symP->histo/(FLOAT)symP->ncalled;
  1048.         t->child_time=(FLOAT)t->ncalls* symP->sub_histo  /(FLOAT)symP->ncalled;
  1049.         (*f)->sub_histo+=t->prop_time+t->child_time;
  1050.       }
  1051.       (*f)->cycnum= -1;
  1052.     }
  1053.  
  1054.     /* move this node past f_end, since we don't care
  1055.        about it anymore */
  1056.     if(f_end!=functions_in_call_tree) {
  1057.       /* move this function to the end */
  1058.       if(f!=f_end) {
  1059.         struct mesym *tmp;
  1060.  
  1061.         tmp= *f;
  1062.         *f= *f_end;
  1063.         *f_end=tmp;
  1064.       }
  1065.       --f_end;
  1066.     }
  1067.     assert(f_end>=functions_in_call_tree);
  1068.       }
  1069.     } while(f-->&functions_in_call_tree[0]);
  1070.   } while(found && f_end>&functions_in_call_tree[0]);
  1071. }
  1072.  
  1073. void
  1074. findcycles(void)
  1075. {
  1076.     struct mesym **f;
  1077.     struct mesym *ptr;
  1078.     struct symlist *tmp;
  1079.     struct cy {
  1080.         struct mesym *memb1;
  1081.         struct mesym *memb2;
  1082.         struct mesym **others;
  1083.         int width;
  1084.         int deepest;
  1085.         int shallowest;
  1086.     };
  1087.     struct cy *cy;
  1088.     int ncy = 0;
  1089.     int n;
  1090.  
  1091.     int bigdepth = 0;
  1092.     int curdepth;
  1093.     struct mesym *current_cycle_pointer;
  1094.     struct mesym *cursym;
  1095.  
  1096.     int tree_depth;
  1097.  
  1098.     init_ring_buffer();
  1099.     for(f= &functions_in_call_tree[0];f<=f_end;f++) {
  1100.         if((*f)->ncalled==0) {
  1101.             push_ring_buffer(*f);
  1102.             (*f)->flag=0;
  1103.         }
  1104.     }
  1105.     push_ring_buffer((void *)0);
  1106.  
  1107.     tree_depth = 1;
  1108.     for(;;) {
  1109.         ptr=pop_ring_buffer();
  1110.         if(!ptr) {
  1111.             if(ring_buffer_isnt_empty()) {
  1112.                 push_ring_buffer((void *)0);
  1113.                 tree_depth ++;
  1114.                 continue;
  1115.             } else {
  1116.                 break;
  1117.             }
  1118.         } else if(ptr->flag==1) {
  1119.             fprintf(stderr,"Ignoring call to spont function %s\n",ptr->name);
  1120.         } else if(ptr->flag!=0) {
  1121.             /* Save upward edge */
  1122.             /* printf("Upward edge detected in function %s\n",ptr->name); */
  1123.             if(!ncy) {
  1124.                 cy=ck_malloc(sizeof (struct cy));
  1125.                 ncy=1;
  1126.             } else {
  1127.                 ncy++;
  1128.                 cy=ck_realloc(cy,ncy*sizeof(struct cy));
  1129.             }
  1130.             cy[ncy-1].memb1=ptr;
  1131.             cy[ncy-1].memb2=0;
  1132.         } else {
  1133.             ptr->flag=tree_depth;
  1134.             for(tmp=ptr->calls;tmp;tmp=tmp->next_to)
  1135.                 if(tmp->sym_to->cycnum!=-1)
  1136.                     push_ring_buffer(tmp->sym_to);
  1137.         }
  1138.     }
  1139.     if(!ncy)
  1140.         return;
  1141.  
  1142.     for(n=0;n<ncy;n++) {
  1143.         struct symlist *s;
  1144.         struct symlist *u;
  1145.  
  1146.  
  1147.         cursym=cy[n].memb1;
  1148.         if(cursym->cycnum)
  1149.             continue;
  1150.  
  1151.         number_of_cycles++;
  1152.         /* for(s=cursym->called;s;s=s->next_from) {
  1153.             if(s->sym_from->flag>=cursym->flag)
  1154.                 break;
  1155.         }
  1156.         if(!s)
  1157.             abort(); */
  1158.  
  1159.         if (!cycles)
  1160.             cycles=(struct mesym **)ck_malloc(sizeof(struct mesym *));
  1161.         else
  1162.             cycles=(struct mesym **)ck_realloc((void *)cycles,
  1163.                                number_of_cycles*sizeof(struct mesym *));
  1164.  
  1165.         current_cycle_pointer = (struct mesym *)ck_malloc(sizeof(struct mesym));
  1166.         cycles[number_of_cycles-1] = current_cycle_pointer;
  1167.         bzero (current_cycle_pointer, sizeof *current_cycle_pointer);
  1168.         current_cycle_pointer->name=mk_sprintf("<cycle %d as a whole>",number_of_cycles);
  1169.         current_cycle_pointer->value= (unsigned long)-1;
  1170.         current_cycle_pointer->cycnum=number_of_cycles;
  1171.  
  1172.         cursym=cy[n].memb1;
  1173.  
  1174.         push_ring_buffer(cursym);
  1175.  
  1176.         while(ring_buffer_isnt_empty()) {
  1177.             cursym=pop_ring_buffer();
  1178.             if(cursym->cycnum==number_of_cycles)
  1179.                 continue;
  1180.             if(cursym->cycnum!=0)
  1181.                 abort();
  1182.             PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_CYC,("adding %s(%d) to cycle",cursym->name,cursym->histo));
  1183.             current_cycle_pointer->histo+=cursym->histo;
  1184.             cursym->cycnum=number_of_cycles;
  1185.             add_to_lists(current_cycle_pointer,cursym,0);
  1186.  
  1187.             /* Now queue the subroutines of this function to be scanned
  1188.                eventually.  */
  1189.  
  1190.             for(u=cursym->calls;u;u=u->next_to)
  1191.                 if(u->sym_to->cycnum==0) {
  1192.                     push_ring_buffer((void *)(u->sym_to));
  1193.                 } else if(u->sym_to->name[0] == '<') {
  1194.                     PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_CYC,("Cycle calls %s(%d)",u->sym_to->name,u->ncalls));
  1195.                     add_to_lists(current_cycle_pointer,u->sym_to,u->ncalls);
  1196.                 }
  1197.         }
  1198.  
  1199.         /* The cycle's list of children now contains all the members of the cycle.
  1200.            Occasionally a function creeps in that doesn't belong in the cycle.
  1201.            Find and remove them. */
  1202.  
  1203.         {
  1204.             struct symlist *v;
  1205.             int found;
  1206.  
  1207.             do {
  1208.                 found = 0;
  1209.                 for(u=current_cycle_pointer->calls;u;u=u->next_to) {
  1210.                     for(v=u->sym_to->called;v;v=v->next_from)
  1211.                         if(v->sym_from->name[0]!='<' && v->sym_from->cycnum==number_of_cycles)
  1212.                             break;
  1213.                     if(!v) {
  1214.                         PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_CYC,("%s not really in cycle",u->sym_to->name));
  1215.                         /* This 'cycle member' wasn't called by any member of the cycle.
  1216.                            thus, it isn't a cycle member. */
  1217.                         u->sym_to->cycnum=0;
  1218.                         delete_from_lists(current_cycle_pointer,u->sym_to);
  1219.                         found++;
  1220.                     }
  1221.                 }
  1222.             } while(found);
  1223.         }
  1224.         /* The cycle's lists of callers and children are now correct.
  1225.            Propagate the time through the cycle.  */
  1226.  
  1227.  
  1228.         /* Now we propagate time INTO the
  1229.            cycle from its children.  We could do this in flushfuns,
  1230.            but its easier to do here, since doing it here guarentees
  1231.            it only happens once per cycle */
  1232.  
  1233.         for(u=current_cycle_pointer->calls;u;u=u->next_to) {
  1234.             struct mesym *symP;
  1235.             struct symlist *v;
  1236.  
  1237.             /* We have to remember to not propagate time that's already here. . . */
  1238.             if(u->sym_to->cycnum!=number_of_cycles) {
  1239.                 current_cycle_pointer->ncalls+=u->ncalls;
  1240.  
  1241.                 if(u->sym_to->cycnum==-1)
  1242.                     symP= u->sym_to;
  1243.                 else            /* Propagate time FROM another cycle here */
  1244.                     symP= cycles[u->sym_to->cycnum-1];
  1245.                 u->prop_time= (FLOAT)u->ncalls*(FLOAT)symP->histo/(FLOAT)symP->ncalled;
  1246.                 u->child_time=(FLOAT)u->ncalls* symP->sub_histo  /(FLOAT)symP->ncalled;
  1247.                 current_cycle_pointer->sub_histo+=u->prop_time+u->child_time;
  1248.                 for(v=u->sym_to->called;v;v=v->next_from) {
  1249.                     if(v->sym_from->cycnum==number_of_cycles) {
  1250.                         v->prop_time= (FLOAT)v->ncalls*(FLOAT)symP->histo/(FLOAT)symP->ncalled;
  1251.                         v->child_time=(FLOAT)v->ncalls*  symP->sub_histo /(FLOAT)symP->ncalled;
  1252.                     }
  1253.                 }
  1254.             } else {
  1255.                 u->prop_time= u->sym_to->histo;
  1256.                 u->child_time=u->sym_to->sub_histo;
  1257.  
  1258.                 for(v=u->sym_to->calls;v;v=v->next_to) {
  1259.                     if(v->sym_to->cycnum==number_of_cycles) {
  1260.                         add_to_lists(current_cycle_pointer,v->sym_to,v->ncalls);
  1261.                         current_cycle_pointer->recursive+=v->ncalls;
  1262.                     } else {
  1263.                         symP = v->sym_to;
  1264.                         v->prop_time= (FLOAT)v->ncalls*(FLOAT)symP->histo/(FLOAT)symP->ncalled;
  1265.                         v->child_time=(FLOAT)v->ncalls*  symP->sub_histo /(FLOAT)symP->ncalled;
  1266.                     }
  1267.                 }
  1268.  
  1269.                 for(v=u->sym_to->called;v;v=v->next_from) {
  1270.                     assert(v->sym_from->cycnum==0 || v->sym_from->cycnum==number_of_cycles);
  1271.                     if(v->sym_from->cycnum==0) {
  1272.  PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_CYC,("Cycle called by %s(%d)",v->sym_from->name,v->ncalls));
  1273.                         add_to_lists(v->sym_from,current_cycle_pointer,v->ncalls);
  1274.                     }
  1275.                 }
  1276.             }
  1277.         }
  1278.  
  1279.  
  1280.         /* Sum all calls into the cycle, from each caller not in the cycle,
  1281.            to get the number of calls into the cycle.  */
  1282.         for(u=current_cycle_pointer->called;u;u=u->next_from) {
  1283.             if(u->sym_from->cycnum!=number_of_cycles)
  1284.                 current_cycle_pointer->ncalled+=u->ncalls;
  1285.         }
  1286.  
  1287.         /* Loop over functions in this cycle.  */
  1288.         for(u=current_cycle_pointer->calls;u;u=u->next_to) {
  1289.             struct symlist *v;
  1290.  
  1291.             if(u->sym_to->cycnum!=number_of_cycles)
  1292.                 continue;
  1293.  
  1294.             /* U->sym_to is a function in this cycle.  */
  1295.  
  1296.             /* Find all calls to this function from functions outside the cycle
  1297.                and add remove them from the count of calls "from the cycle" to this function.  */
  1298.  
  1299.             for(v=u->sym_to->called;v;v=v->next_from) {
  1300.                 if(v->sym_from != current_cycle_pointer
  1301.                    && v->sym_from->cycnum==number_of_cycles)
  1302.                     u->sym_to->ncalled-=v->ncalls;
  1303.             }
  1304.         }
  1305.  
  1306.         /* Compute amounts of time to propagate out of the cycle
  1307.            to the callers-in.  */
  1308.  
  1309.         for (u=current_cycle_pointer->called;u;u=u->next_from)
  1310.             if (u->sym_from->cycnum != number_of_cycles) {
  1311.                 struct symlist *v;
  1312.  
  1313.                 u->prop_time= ((FLOAT)u->ncalls*(FLOAT)current_cycle_pointer->histo     /(FLOAT)current_cycle_pointer->ncalled);
  1314.                 u->child_time=((FLOAT)u->ncalls*       current_cycle_pointer->sub_histo /(FLOAT)current_cycle_pointer->ncalled);
  1315.                 for(v=u->sym_from->calls;v;v=v->next_to) {
  1316.                     if(v->sym_to->cycnum==number_of_cycles) {
  1317.                         v->prop_time= ((FLOAT)v->ncalls*(FLOAT)current_cycle_pointer->histo     /(FLOAT)current_cycle_pointer->ncalled);
  1318.                         v->child_time=((FLOAT)v->ncalls*       current_cycle_pointer->sub_histo /(FLOAT)current_cycle_pointer->ncalled);
  1319.                     }
  1320.                 }
  1321.             }
  1322.  
  1323.         flushfuns();
  1324.     }
  1325. }
  1326.  
  1327.  
  1328. /* Remove from the call tree all nodes that are rejected by
  1329.    the -e, -E, -f and -F filters that were specified.
  1330.    Their nodes are removed from functions_in_call_tree
  1331.    and their edges are deleted from the lists they are in.  */
  1332.  
  1333. void
  1334. filter_graph ()
  1335. {
  1336.   int n;
  1337.   int the_bomb_is_falling = 0;
  1338.  
  1339.   for(n=0;n<nfilters;n++) {
  1340.     struct mesym **call_tree_pointer;
  1341.  
  1342.     call_tree_pointer=find_funp_from_name(filters[n].name);
  1343.     /* Couldn't find it?  Skip it! */
  1344.     if(!call_tree_pointer) {
  1345.       /* It may have taken time, although it
  1346.      isn't in the call tree.  Seek and
  1347.      destroy! */
  1348.       if(filters[n].type==BIG_E) {
  1349.     struct mesym *p;
  1350.  
  1351.     for(p=syms;p< &syms[nsym];p++) {
  1352.       if(!strcmp(p->name,filters[n].name)) {
  1353.         tothist-=p->histo;
  1354.         break;
  1355.       }
  1356.     }
  1357.     if(p==&syms[nsym])
  1358.       fprintf(stderr,"Warning: couldn't find function %s\n",filters[n].name);
  1359.       } else
  1360.     fprintf(stderr,"Warning:  %s is not in the call tree.\n",filters[n].name);
  1361.       continue;
  1362.     }
  1363.     PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_OPT,("Option %d on %s",filters[n].type,(*call_tree_pointer)->name));
  1364.  
  1365.     switch(filters[n].type) {
  1366.     case SMALL_E:
  1367.       kill_children(*call_tree_pointer,FALSE);
  1368.       break;
  1369.     case BIG_E:
  1370.       kill_children(*call_tree_pointer,TRUE);
  1371.       break;
  1372.     case SMALL_F:
  1373.       if(!the_bomb_is_falling)
  1374.     the_bomb_is_falling = 1;
  1375.       save_the_children(*call_tree_pointer,FALSE);
  1376.       break;
  1377.     case BIG_F:
  1378.       if(!the_bomb_is_falling) {
  1379.     the_bomb_is_falling = 1;
  1380.     tothist=0;
  1381.       }
  1382.       save_the_children(*call_tree_pointer,TRUE);
  1383.       break;
  1384.     default:
  1385.       abort();
  1386.     }
  1387.   }
  1388.  
  1389.   /* If we had -e or -E filters, now delete everything that
  1390.      was not marked to be saved.  */
  1391.  
  1392.   if(the_bomb_is_falling) {
  1393.     struct mesym **call_tree_pointer;
  1394.  
  1395.     PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_OPT,("And now we drop the bomb."));
  1396.     call_tree_pointer=&functions_in_call_tree[number_of_functions_in_call_tree-1];
  1397.     do {
  1398.       if(((*call_tree_pointer)->flag&SAVE_ME)==0)
  1399.     remove_from_call_tree(call_tree_pointer);
  1400.     } while (call_tree_pointer-->&functions_in_call_tree[0]);
  1401.   }
  1402. }
  1403.  
  1404. /* Collect a list of parents/children, sort them, and print them */
  1405. /* If CALLED > 0, we print parents,
  1406.       and the value of CALLED is the total number of times this fn was called.
  1407.    If CALLED == -2 we print children.
  1408.       This is used for printing the children of an entire cycle.
  1409.    If CALLED == -1, we print children and if their cycle number is the
  1410.       same as CYCLE, we print abbreviated information.
  1411.       This is used for printing the children of an ordinary function.  */
  1412. /* LIST is the list of parents/children we want to print */
  1413.  
  1414. void
  1415. print_sorted_list(int called,int cycle,struct symlist *list)
  1416. {
  1417.   static struct symlist **sbuf;
  1418.   static nsbuf,sizsbuf;
  1419.   struct symlist **sbp,*t;
  1420.   struct mesym *symP;
  1421.  
  1422.   if(!sizsbuf) {
  1423.     sbuf=(struct symlist **)ck_calloc(30,sizeof(struct symlist *));
  1424.     nsbuf=0;
  1425.     sizsbuf=30;
  1426.   }
  1427.  
  1428.   /* Extract all the symbols in LIST as a vector,
  1429.      but ignore any which represent entire cycles.  */
  1430.  
  1431.   t=list;
  1432.   for(sbp= &sbuf[0];t;) {
  1433.     symP = (called>=0) ? t->sym_from : t->sym_to;
  1434.     if(symP && symP->name[0] != '<') {
  1435.       *sbp=t;
  1436.       sbp++;
  1437.       nsbuf++;
  1438.       if(nsbuf==sizsbuf) {
  1439.     sizsbuf*=2;
  1440.     sbuf=(struct symlist **)ck_realloc((void *)sbuf,sizsbuf*sizeof(struct symlist *));
  1441.     sbp= &sbuf[nsbuf];
  1442.       }
  1443.     } else {
  1444.       symP++;
  1445.             
  1446.     }
  1447.  
  1448.     if(called>=0) t=t->next_from;
  1449.     else t=t->next_to;
  1450.   }
  1451.  
  1452.   /* Sort the vector.  */
  1453.   qsort(sbuf,nsbuf,sizeof(struct symlist *),listcmp);
  1454.  
  1455.   /* Print the elements of the vector.  */
  1456.   for(sbp= &sbuf[0];nsbuf>0;nsbuf--,sbp++) {
  1457.     t= *sbp;
  1458.     if(called>=0) symP=t->sym_from;
  1459.     else symP=t->sym_to;
  1460.  
  1461.     if(cycle>0 && cycle==symP->cycnum) {
  1462.       if(called==-2) {
  1463.     printf("              %7.2f  %7.2f %4d          %s",
  1464.            convert_and_round((FLOAT)(symP->histo)),
  1465.            convert_and_round((FLOAT)(symP->sub_histo)),
  1466.            t->ncalls,
  1467.            symP->name);
  1468.       } else
  1469.     /* For things in the same cycle, we only want to print
  1470.        the number of calls */
  1471.     printf("%30s %4d          %s","", t->ncalls, symP->name);
  1472.     } else {
  1473.       printf("              %7.2f  %7.2f %4d/%-4d     %s",
  1474.          convert_and_round(t->prop_time),
  1475.          convert_and_round(t->child_time),
  1476.          t->ncalls,
  1477.          (called>=0) ? called : symP->ncalled,
  1478.          symP->name);
  1479.     }
  1480.  
  1481.     if(symP->cycnum>0 && symP->name[0]!='<')
  1482.       printf(" <cycle %d>",symP->cycnum);
  1483.  
  1484.     if(symP->numindex)
  1485.       printf(" [%d]\n",symP->numindex);
  1486.     else
  1487.       printf(" [not printed]\n");
  1488.   }
  1489. }
  1490.  
  1491. /* Compare two symbols for which should come first among
  1492.    the callers or subroutines in a single call-graph entry.  */
  1493.  
  1494. int
  1495. listcmp(void *a,void *b)
  1496. {
  1497.   struct symlist *aa,*bb;
  1498.   FLOAT    n;
  1499.  
  1500.   aa= *(struct symlist **)a;
  1501.   bb= *(struct symlist **)b;
  1502.  
  1503.   n=(bb->prop_time + bb->child_time - aa->prop_time - aa->child_time);
  1504.  
  1505.   if(n<0) return -1;
  1506.   if(n>0) return 1;
  1507.   return 0;
  1508. }
  1509.  
  1510. /* Convert a number (IN) from the histogram into seconds, rounding to the
  1511.    nearest 100th of a second. */
  1512. FLOAT
  1513. convert_and_round(FLOAT in)
  1514. {
  1515.   long int inter;
  1516.  
  1517.   inter=((in/(FLOAT)(ticks))+.005)*100.0;
  1518.   return ((FLOAT)(inter)/100.0);
  1519. }
  1520.  
  1521. /* Given ncalls from fromP to toP, add a symlist element telling about it */
  1522. void
  1523. add_to_lists(struct mesym *fromP,struct mesym *toP,unsigned ncalls)
  1524. {
  1525.   struct symlist *tmp;
  1526.  
  1527.   for(tmp=fromP->calls;tmp;tmp=tmp->next_to)
  1528.     if(tmp->sym_to==toP) {
  1529.       tmp->ncalls+=ncalls;
  1530.       break;
  1531.     }
  1532.   if(!tmp) {
  1533.     tmp=(struct symlist *)ck_malloc(sizeof(struct symlist));
  1534.     tmp->sym_from=fromP;
  1535.     tmp->next_from=toP->called;
  1536.     tmp->sym_to=toP;
  1537.     tmp->next_to=fromP->calls;
  1538.     tmp->ncalls=ncalls;
  1539.     tmp->prop_time = -1;
  1540.     tmp->child_time = -1;
  1541.  
  1542.     fromP->calls=tmp;
  1543.     toP->called=tmp;
  1544.   }
  1545. }
  1546.  
  1547.  
  1548. /* The reverse of add_to_lists.  Forget that fromP ever called toP */
  1549. void
  1550. delete_from_lists(struct mesym *fromP, struct mesym *toP)
  1551. {
  1552.   struct symlist *die,*tmp,*old;
  1553.  
  1554.   if(fromP->calls->sym_to==toP) {
  1555.     die=fromP->calls;
  1556.     fromP->calls=fromP->calls->next_to;
  1557.   } else {
  1558.     old=0;
  1559.     for(tmp=fromP->calls;tmp;tmp=tmp->next_to) {
  1560.       if(tmp->sym_to==toP) {
  1561.         die=tmp;
  1562.     old->next_to=tmp->next_to;
  1563.       }
  1564.       old=tmp;
  1565.     }
  1566.   }
  1567.  
  1568.   if(toP->called->sym_from==fromP) {
  1569.     die=toP->called;
  1570.     toP->called=toP->called->next_from;
  1571.   } else {
  1572.     for(tmp=toP->called;tmp;tmp=tmp->next_from) {
  1573.       old=0;
  1574.       if(tmp->sym_from==fromP) {
  1575.         die=tmp;
  1576.     old->next_from=tmp->next_from;
  1577.       }
  1578.       old=tmp;
  1579.     }
  1580.   }
  1581.   free(die);
  1582. }
  1583.  
  1584.  
  1585. /* Implement the -e or -E option by deleting a certain function
  1586.    and all its descendents from the call graph.
  1587.    If FLUSHFLAG is set, remove its histogram time from the total, too. */
  1588.  
  1589. void
  1590. kill_children(struct mesym *p,int flushflag)
  1591. {
  1592.   struct symlist *t;
  1593.  
  1594.   push_ring_buffer(p);
  1595.   while(ring_buffer_isnt_empty()) {
  1596.     p=pop_ring_buffer();
  1597.     if(flushflag)
  1598.       tothist-=p->histo;
  1599.     PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_OPT,("Flushing %s (%d)",p->name,flushflag ? p->histo : 0));
  1600.     p->flag|=KILL_ME;
  1601.     remove_from_call_tree(find_funp_from_pointer(p));
  1602.     for(t=p->calls;t;t=t->next_to) {
  1603.       if(t->sym_to->ncalled==t->ncalls || (p->cycnum>0 && t->sym_to->cycnum==p->cycnum)) {
  1604.     push_ring_buffer(t->sym_to);
  1605.       } else if(t->sym_to->cycnum>0 && t->sym_to->cycnum!=p->cycnum) {
  1606.     if(cycles[t->sym_to->cycnum-1]->ncalled==t->ncalls) {
  1607.       push_ring_buffer(cycles[t->sym_to->cycnum-1]);
  1608.     }
  1609.       } else {
  1610.     struct symlist *ztmp;
  1611.  
  1612.     for(ztmp=t->sym_to->called;ztmp;ztmp=ztmp->next_from) {
  1613.       if((ztmp->sym_from->flag&KILL_ME)==0)
  1614.         break;
  1615.     }
  1616.     if(!ztmp)
  1617.       push_ring_buffer(t->sym_to);
  1618.     else {
  1619.       PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_OPT,("Not flushing %s. . .  More parents",t->sym_to->name));
  1620.       /* Do we want to deal with removing FRACTIONS of time from
  1621.          functions who were called from different places?  If so,
  1622.          code goes here.  (F'rinstance, if half of the calls to
  1623.          function X were made from here, cut its stored time in half */
  1624.     }
  1625.       }
  1626.     }
  1627.   }
  1628. }
  1629.  
  1630. /* This is the opposite of kill_children().
  1631.    -f or -F has been specified, so all call-graph nodes EXCEPT
  1632.    the descendents of specified functions will be killed.
  1633.    Find all these descendents and mark them to be saved
  1634.    by setting the `flag' fields nonzero.
  1635.  
  1636.    If TIMEFLAG is set, the histogram-total has
  1637.    already been nuked, and we should add our histogram time to
  1638.    it in an attempt at reconstruction. . .  */
  1639.  
  1640. void
  1641. save_the_children(struct mesym *p,int timeflag)
  1642. {
  1643.   struct symlist *t;
  1644.  
  1645.   push_ring_buffer(p);
  1646.   while(ring_buffer_isnt_empty()) {
  1647.     p=pop_ring_buffer();
  1648.     PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_OPT,("Saving %s (%d)",p->name,p->histo));
  1649.     p->flag|=SAVE_ME;
  1650.     if(p->cycnum>0)
  1651.       cycles[p->cycnum-1]->flag|=SAVE_ME;
  1652.     if(timeflag)
  1653.       tothist+=p->histo;
  1654.     for(t=p->calls;t;t=t->next_to) {
  1655.       if((t->sym_to->flag&SAVE_ME)==0)
  1656.     push_ring_buffer(t->sym_to);
  1657.     }
  1658.   }
  1659. }
  1660.  
  1661. /* The bomb is falling, and PT has just been hit by severe doses of
  1662.    radiation.  Remove it from the call-tree vector */
  1663. void
  1664. remove_from_call_tree(struct mesym **pt)
  1665. {
  1666.   if(!pt) {
  1667.     /* Just quietly returning allows us to remove cycles from
  1668.        the call tree easily. */
  1669.     /* panic("Internal Error:  trying to remove null from call tree"); */
  1670.     return;
  1671.   }
  1672.   PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_OPT,("Removing %ss from the tree",(*pt)->name));
  1673.   *pt=functions_in_call_tree[number_of_functions_in_call_tree-1];
  1674.   number_of_functions_in_call_tree--;
  1675. }
  1676.  
  1677. /* We want to play with a member of the call tree, but we
  1678.    only know its name.  Try to find the funp.  This is slow,
  1679.    natch, since it uses linear search, but it doesn't get called much */
  1680.  
  1681. /* Should be some way to scan rest of symbols so we could delete
  1682.    mcount/mcleanup histogram ticks.  Should be way to disable warning? */
  1683. struct mesym **
  1684. find_funp_from_name(char *name)
  1685. {
  1686.   struct mesym **ret;
  1687.  
  1688.   ret= &functions_in_call_tree[number_of_functions_in_call_tree-1];
  1689.   do {
  1690.     if(strcmp((*ret)->name,name)==0)
  1691.       return ret;
  1692.   } while (ret-->&functions_in_call_tree[0]);
  1693.   return 0;
  1694. }
  1695.  
  1696. /* We have the mesym, but we need to know where it lives in the call-tree
  1697.    This is almost as slow as find_funp_from_name(). */
  1698. struct mesym **
  1699. find_funp_from_pointer(struct mesym *p)
  1700. {
  1701.   struct mesym **ret;
  1702.  
  1703.   ret= &functions_in_call_tree[number_of_functions_in_call_tree-1];
  1704.   do {
  1705.     if(*ret==p)
  1706.       return ret;
  1707.   } while (ret-->&functions_in_call_tree[0]);
  1708.   /* fprintf(stderr,"Warning:  Can't find %s in the call tree.\n",p->name); */
  1709.   return 0;
  1710. }
  1711.  
  1712. /* Read symbols from a.out file open on FP.  There are N of them.  Allocate a
  1713.    vector to store the interesting ones in, and sort it numerically.  */
  1714.  
  1715. void
  1716. read_syms(FILE *fp,int n)
  1717. {
  1718.   struct nlist *tmpsyms;
  1719.   struct nlist *sym;
  1720.   int i;
  1721.   char buf[n];
  1722.  
  1723.   /* Read the entire symbol table.  */
  1724.   tmpsyms=(struct nlist *)ck_malloc(n*sizeof(struct nlist));
  1725.   ck_fread(tmpsyms,sizeof(struct nlist),n,fp);
  1726.  
  1727.   bzero (buf, n);
  1728.  
  1729.   /* Count the useful symbols.
  1730.      Also relocate their name-fields to be C string pointers.  */
  1731.   for(sym= &tmpsyms[0],i=0;sym<&tmpsyms[n];sym++) {
  1732.     sym->n_un.n_name= strs+sym->n_un.n_strx;
  1733.     if(!badsym(sym))
  1734.       buf[sym-tmpsyms] = 1, i++;
  1735.   }
  1736.  
  1737.   /* Allocate permanent space and copy useful symbols into it.  */
  1738.   nsym=i;
  1739.   syms=(struct mesym *)ck_calloc(nsym,sizeof(struct mesym));
  1740.  
  1741.   for(sym= &tmpsyms[0],i=0;sym<&tmpsyms[n];sym++) {
  1742.     if(!badsym(sym)) {
  1743.       syms[i].name=sym->n_un.n_name;
  1744.  
  1745.       /* Remove the initial _ from the symbol name */
  1746.       if(*(syms[i].name)=='_')
  1747.     syms[i].name++;
  1748.       syms[i].value=sym->n_value;
  1749.       i++;
  1750.     }
  1751.     else if (buf[sym-tmpsyms])
  1752.       abort ();
  1753.   }
  1754.  
  1755.   if (i != nsym)
  1756.     abort ();
  1757.  
  1758.   /* Put symbols in numeric order.  */
  1759.   qsort(syms,nsym,sizeof(struct mesym),symcmp);
  1760.   free((void *)tmpsyms);
  1761. }
  1762.  
  1763. /* Return the symbol which has the largest value less than VAL.
  1764.    Since the symbol vector is sorted by value, this is done
  1765.    with a binary search.  */
  1766.  
  1767. struct mesym *
  1768. val_to_sym(unsigned long val)
  1769. {
  1770.   struct mesym *m;
  1771.   int gap=nsym/4;
  1772.  
  1773.   m= &syms[nsym/2];
  1774.   for(;;) {
  1775.     if(m->value>val) {
  1776.       m-=gap;
  1777.       gap/=2;
  1778.     } else if((m+1)->value<val) {
  1779.       m+=gap;
  1780.       gap/=2;
  1781.     } else
  1782.       break;
  1783.     if(m<&syms[0] || m>=&syms[nsym])
  1784.       abort ();
  1785.     if(gap<1)
  1786.       gap=1;
  1787.   }
  1788.   return m;
  1789. }
  1790.  
  1791. /* Return TRUE if the nlist-entry SYM describes a symbol
  1792.    that gprof should pay attention to.  */
  1793.  
  1794. int
  1795. badsym(struct nlist *sym)
  1796. {
  1797.   if ((sym->n_type & ~N_EXT) != N_TEXT)
  1798.     return TRUE;
  1799.   if (no_locals && !(sym->n_type&N_EXT))
  1800.     return TRUE;
  1801.   /* Filenames or pascal labels should be ignored */
  1802.   if(index(sym->n_un.n_name,'.') || index(sym->n_un.n_name,'$'))
  1803.     return TRUE;
  1804.   return FALSE;
  1805. }
  1806.  
  1807. /* This is used to qsort() the symbol table into numerical order. */
  1808.  
  1809. int
  1810. symcmp(void *a,void *b)
  1811. {
  1812.   struct mesym *aa,*bb;
  1813.  
  1814.   aa=(struct mesym *)a;
  1815.   bb=(struct mesym *)b;
  1816.   return aa->value - bb->value;
  1817. }
  1818.  
  1819. /* This is used to sort the vector for the flat profile.
  1820.    if they used different amounts of time, the one with
  1821.    the most time goes first.  If they used the same amount,
  1822.    the one that was called most goes first.  If they were
  1823.    called the same #, they are sorted alphabetically */
  1824.  
  1825. int
  1826. timecmp(void *a,void *b)
  1827. {
  1828.   struct mesym *aa,*bb;
  1829.   int    n;
  1830.  
  1831.   aa= *(struct mesym **)a;
  1832.   bb= *(struct mesym **)b;
  1833.   n = bb->histo - aa->histo;
  1834.   if(n==0) n=bb->ncalled - aa->ncalled;
  1835.   if(n==0) n=strcmp(aa->name,bb->name);
  1836.   return n;
  1837. }
  1838.  
  1839. /* This is used to sort the functions_in_call_tree[] vector
  1840.    so the leaf nodes are at the end, and the root nodes
  1841.    are at the beginning.  This bit about useless nodes could
  1842.    probably be taken out now, since they shouldn't make it
  1843.    into the vector in the first place */
  1844.  
  1845. int
  1846. callcmp(void *a,void *b)
  1847. {
  1848.   struct mesym *aa,*bb;
  1849.  
  1850.   aa= *(struct mesym **)a;
  1851.   bb= *(struct mesym **)b;
  1852.  
  1853.   /* Send useless symbols to the end */
  1854.   if(aa->ncalls==0 && aa->ncalled==0) {
  1855.     if(bb->ncalls==0 && bb->ncalled==0)
  1856.       return EQ;
  1857.     return GT;
  1858.   }
  1859.   if(bb->ncalled==0 && bb->ncalls==0)
  1860.     return LT;
  1861.  
  1862.   /*     send root nodes to the front; */
  1863.   /* Functions that were called 0 times
  1864.      are spontaneous */
  1865.  
  1866.   if(aa->ncalled==0) {
  1867.     if(bb->ncalled==0)
  1868.       return EQ;
  1869.     return LT;
  1870.   }
  1871.   if(bb->ncalled==0)
  1872.     return GT;
  1873.  
  1874.   /* Send leaf nodes to the end */
  1875.   if(aa->ncalls==0) {
  1876.     if(bb->ncalls==0)
  1877.       return EQ;
  1878.     return GT;
  1879.   }
  1880.   if(bb->ncalls==0)
  1881.     return LT;
  1882.  
  1883.   /* And keep the rest the same */
  1884.   return EQ;
  1885. }
  1886.  
  1887. /* This is used to sort functions_in_call_tree[] before printing.
  1888.    To print, we want
  1889.    A:    the ones with the most time first
  1890.        a:    (If one was never called, put it first, 'cuz it
  1891.         was invoked by GOD, else the one called the most
  1892.          # of times first)
  1893.         1:  the one with the lower name (strcmp()) first
  1894.  */
  1895.  
  1896. int
  1897. treetimecmp(void *a,void *b)
  1898. {
  1899.   struct mesym *aa,*bb;
  1900.   int    n;
  1901.  
  1902.   aa= *(struct mesym **)a;
  1903.   bb= *(struct mesym **)b;
  1904.   n = (bb->histo+bb->sub_histo) - (aa->histo+aa->sub_histo);
  1905.   if(n==0) {
  1906.  
  1907.     /* Just to be bizarre:  If one was never called,
  1908.        put it first, else put the one who was called
  1909.        the most first.  Confused yet? */
  1910.     if(aa->ncalled==0)
  1911.       return LT;
  1912.     if(bb->ncalled==0)
  1913.       return GT;
  1914.     n=bb->ncalled - aa->ncalled;
  1915.     if(n==0)
  1916.       n=strcmp(aa->name,bb->name);
  1917.   }
  1918.   return n;
  1919. }
  1920.  
  1921. /* Read in a gmon.out file named NAME and deal with the stuff inside it.  */
  1922.  
  1923. void
  1924. readgm(char *name)
  1925. {
  1926.   FILE *fp;
  1927.   struct gm_header tmp;
  1928.   unsigned CHUNK *p;
  1929.   int    n;
  1930.   struct gm_call calltmp;
  1931.  
  1932.   PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_GFILE,("gmon from `%s'",name));
  1933.  
  1934.   fp=ck_fopen(name,"r");
  1935.  
  1936.   /* Read in the gmon file header and check that histogram is
  1937.      compatible with the other gmon files already read.  */
  1938.  
  1939.   ck_fread((void *)&tmp,sizeof(tmp),1,fp);
  1940.   if(hdr.low) {
  1941.     if(hdr.low!=tmp.low || hdr.high!=tmp.high)
  1942.       fatal("file `%s' is incompatable with previous gmon.out file",name);
  1943.   } else
  1944.     hdr=tmp;
  1945.  
  1946.   PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_GFILE,("lowpc=%ld  hipc=%ld  nbytes= %ld",hdr.low,hdr.high,hdr.nbytes));
  1947.  
  1948.   /* Allocate the total histogram if we haven't yet done so.  */
  1949.  
  1950.   if(!histo) {
  1951.     nhist=(hdr.nbytes-sizeof(struct gm_header))/sizeof(CHUNK);
  1952.     histo=(unsigned CHUNK *)ck_calloc(nhist,sizeof(unsigned CHUNK));
  1953.   }
  1954.  
  1955.   /* Read this file's histogram and merge it into the total one.  */
  1956.  
  1957.   p=(unsigned CHUNK *)ck_malloc(nhist*sizeof(unsigned CHUNK));
  1958.   ck_fread((void *)p,sizeof(unsigned CHUNK),nhist,fp);
  1959.   for(n=0;n<nhist;n++) {
  1960.     tothist+=p[n];
  1961.     histo[n]+=p[n];
  1962.     if(debug&DB_GFILE) {
  1963.       static ncol;
  1964.  
  1965.       if(n==0) ncol=0;
  1966.       if(histo[n]) {
  1967.     fprintf(stderr,"s[%05d]=%-3d",n,histo[n]);
  1968.     if(ncol++%10==9) fputc('\n',stderr);
  1969.       }
  1970.     }
  1971.   }
  1972.   free((void *)p);
  1973.   PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_GFILE,(""));
  1974.  
  1975.   /* We've read in the histogram, now read in the function call data.
  1976.      Loop, reading one call-graph edge from the file
  1977.      and recording the edge in the graph.  */
  1978.  
  1979.   while(fread((void *)&calltmp,sizeof(calltmp),1,fp)==1) {
  1980.     struct symlist *tmp;
  1981.     struct mesym *s_fm,*s_to;
  1982.  
  1983.     /* Find the calling and called functions's symbol entries.  */
  1984.  
  1985.     s_fm=val_to_sym(calltmp.from);
  1986.     s_to=val_to_sym(calltmp.to);
  1987.  
  1988.     if(!s_fm || !s_to) {
  1989.       PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_GFILE,("unknown call %#lx to %#lx %d times.",calltmp.from,calltmp.to,calltmp.ncalls));
  1990.       continue;
  1991.     }
  1992.  
  1993.     /* Updated total numbers of calls from this caller and to this callee.  */
  1994.  
  1995.     s_to->ncalled+=calltmp.ncalls;
  1996.     s_fm->ncalls+=calltmp.ncalls;
  1997.  
  1998.     /* Add these calls to the edge between them.  */
  1999.  
  2000.     add_to_lists(s_fm,s_to,calltmp.ncalls);
  2001.  
  2002.     PRINT_OBNOXIOUS_DEBUG_MESSAGE
  2003.       (DB_GFILE,("call %s (%#lx) to %s (%#lx) %ld times",s_fm->name,
  2004.          calltmp.from,s_to->name,calltmp.to,calltmp.ncalls));
  2005.   }
  2006.  
  2007.   ck_fclose(fp);
  2008. }
  2009.  
  2010. /* Print one of those obnoxious and vaguely informative blurbs that we know
  2011.    and love so well.  Used to be, we'd print them out of a file, but nowaday
  2012.    the blurb is encoded direcly into the program.  Makes printing them
  2013.    much faster.  Also means bozo syswiz can't accidentally delete/move/etc the
  2014.    blurb file on us.  */
  2015.  
  2016. void
  2017. print_blurb(char *blurb)
  2018. {
  2019.   if(! no_blurbs)
  2020.     fputs(blurb,stdout);
  2021. }
  2022.  
  2023.  
  2024. /* Find the number of clock ticks/second by reading the kernel's memory.
  2025.    This means that if /dev/kmem isn't readable, this program will have to run
  2026.    set[ug]id to someone who can read the kernel.   setgid is probably best.  */
  2027.  
  2028. long
  2029. get_ticks(void)
  2030. {
  2031.   static struct nlist nl[2];
  2032.   long    ret;
  2033.   FILE    *fp;
  2034.  
  2035.   nl[0].n_un.n_name="_hz";
  2036.   nlist("/vmunix",&nl[0]);
  2037.   if(nl[0].n_type==0)
  2038.     fatal("Can't find `%s' in namelist of /vmunix",nl[0].n_un.n_name);
  2039.   fp=ck_fopen("/dev/kmem","r");
  2040.   ck_fseek(fp,(long)nl[0].n_value,0);
  2041.   ck_fread((void *)&ret,sizeof(ret),1,fp);
  2042.   ck_fclose(fp);
  2043.   PRINT_OBNOXIOUS_DEBUG_MESSAGE(DB_MISC,("get_ticks()=%ld",ret));
  2044.   return ret;
  2045. }
  2046.  
  2047.  
  2048. /* Record one -e, -E, -f or -F option in `filters', and check for conflicts.
  2049.    All these options are recorded there for processing later
  2050.    once the call-graph has been constructed.  */
  2051.  
  2052. void
  2053. add_filter(char *name,int type)
  2054. {
  2055.   if(!filters) {
  2056.     filters=(struct filter *)ck_malloc(sizeof(struct filter));
  2057.     nfilters=1;
  2058.   } else {
  2059.     int    n;
  2060.  
  2061.     for(n=0;n<nfilters;n++) {
  2062.       if(filters[n].name==name || !strcmp(filters[n].name,name))
  2063.     fatal("Conflicting options for function `%s'", name);
  2064.     }
  2065.     nfilters++;
  2066.     filters=(struct filter *)ck_realloc((void *)filters,sizeof(struct filter)*nfilters);
  2067.   }
  2068.   filters[nfilters-1].name=name;
  2069.   filters[nfilters-1].type=type;
  2070. }
  2071.  
  2072. /* Data base associating stdio streams with the file names that are open.  */
  2073.  
  2074. struct file_to_name {
  2075.     FILE *stream;    /* A stdio stream */
  2076.     char *name;    /* The file name (malloc'd specially for this list) */
  2077.     struct file_to_name *next;
  2078. };
  2079.  
  2080. struct file_to_name *files_to_names;
  2081.  
  2082. /* Given a stdio stream, look it up in the data base and return
  2083.    the file name.  */
  2084.  
  2085. char *
  2086. stream_name(FILE *stream)
  2087. {
  2088.   struct file_to_name *tail;
  2089.  
  2090.   for (tail = files_to_names; tail; tail = tail->next)
  2091.     if (tail->stream == stream)
  2092.       return tail->name;
  2093.  
  2094.   return "unknown file";
  2095. }
  2096.  
  2097. /* Open a file like `fopen', but report a fatal error if it fails;
  2098.    if it succeeds, record the stream and filename in the data base of such.  */
  2099.  
  2100. FILE *
  2101. ck_fopen(char *name,char *mode)
  2102. {
  2103.   FILE *stream;
  2104.   int n;
  2105.   struct file_to_name *new;
  2106.  
  2107.   stream=fopen(name,mode);
  2108.   if(stream==(FILE *)0)
  2109.     fatal_io ("Couldn't open", name);
  2110.  
  2111.   new = ck_malloc (sizeof (struct file_to_name));
  2112.   new->name = ck_malloc (strlen (name) + 1);
  2113.   strcpy (new->name, name);
  2114.   new->stream = stream;
  2115.   new->next = files_to_names;
  2116.   files_to_names = new;
  2117.   return stream;
  2118. }
  2119.  
  2120. /* Interfaces to various functions of stdio
  2121.    which use the stream/filename database to print an error message
  2122.    if the function gets an error.  */
  2123.  
  2124. void
  2125. ck_fseek(FILE *stream,long i,int w)
  2126. {
  2127.   if(fseek(stream,i,w)==-1)
  2128.     fatal_io ("Couldn't lseek", stream_name(stream));
  2129. }
  2130.  
  2131. void
  2132. ck_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
  2133. {
  2134.   if(fread(ptr,size,nmemb,stream)!=nmemb)
  2135.     fatal_io ("Couldn't read", stream_name(stream));
  2136. }
  2137.  
  2138. void
  2139. ck_fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream)
  2140. {
  2141.   if(fwrite(ptr,size,nmemb,stream)!=nmemb)
  2142.     fatal_io ("Couldn't write", stream_name(stream));
  2143. }
  2144.  
  2145. void
  2146. ck_fclose(FILE *stream)
  2147. {
  2148.   struct file_to_name *tail;
  2149.  
  2150.   fflush (stream);
  2151.   if(ferror (stream))
  2152.     fatal ("I/O error on `%s'", stream_name (stream));
  2153.   if(fclose(stream)==EOF)
  2154.     fatal_io ("Couldn't close", stream_name(stream));
  2155.  
  2156.   if (files_to_names && files_to_names->stream == stream)
  2157.     files_to_names = files_to_names->next;
  2158.   else
  2159.     for (tail = files_to_names; tail->next; tail = tail->next)
  2160.       if (tail->next->stream == stream)
  2161.     {
  2162.       struct file_to_name *loser = tail->next;
  2163.       tail->next = tail->next->next;
  2164.       free (loser->name);
  2165.       free (loser);
  2166.       return;
  2167.     }
  2168. }
  2169.  
  2170. /* Report a fatal error doing I/O, and exit.
  2171.    PROBLEM is "Couldn't whatever" and NAME is the file name.  */
  2172.  
  2173. void
  2174. fatal_io (char *problem, char *name)
  2175. {
  2176.   fprintf (stderr, "%s: %s %s:", myname, problem, name);
  2177.   perror (0);
  2178.   exit (1);
  2179. }
  2180.  
  2181. /* Report a fatal error and exit. Arguments like `printf'.  */
  2182.  
  2183. void
  2184. fatal(char *s,...)
  2185. {
  2186.   va_list iggy;
  2187.  
  2188.   va_start(iggy,s);
  2189.   fprintf(stderr,"%s:",myname);
  2190.   vfprintf(stderr,s,iggy);
  2191.   putc('\n',stderr);
  2192.   va_end(iggy);
  2193.  
  2194.   exit (1);
  2195. }
  2196.  
  2197. /* Memory allocation functions.  */
  2198.  
  2199. /* This function is called just like `printf'
  2200.    except that the output is put into a new string
  2201.    allocated with `malloc'.
  2202.    Returns the address of the string.  The caller must free the string.  */
  2203.  
  2204. char *
  2205. mk_sprintf(char *str,...)
  2206. {
  2207.   va_list iggy;
  2208.   char tmpbuf[2048];
  2209.   char *ret;
  2210.  
  2211.   va_start(iggy,str);
  2212.   vsprintf(tmpbuf,str,iggy);
  2213.   va_end(iggy);
  2214.  
  2215.   ret=ck_malloc(strlen(tmpbuf)+1);
  2216.   strcpy(ret,tmpbuf);
  2217.   return ret;
  2218. }
  2219.  
  2220. /* Encapsulations of `malloc', `calloc' and `realloc'
  2221.    that cause fatal errors if there is not enough memory.  */
  2222.  
  2223. void *
  2224. ck_malloc(size_t size)
  2225. {
  2226.   void *ret;
  2227.   void *malloc();
  2228.  
  2229.   ret=malloc(size);
  2230.   if(ret==(void *)0)
  2231.     fatal ("Virtual memory exhausted");
  2232.   return ret;
  2233. }
  2234.  
  2235. void *
  2236. ck_calloc(size_t nmemb,size_t size)
  2237. {
  2238.   void *ret;
  2239.   void *calloc();
  2240.  
  2241.   ret=calloc(nmemb,size);
  2242.   if(ret==(void *)0)
  2243.     fatal ("Virtual memory exhausted");
  2244.   return ret;
  2245. }
  2246.  
  2247. void *
  2248. ck_realloc(void *ptr,size_t size)
  2249. {
  2250.   void *ret;
  2251.   void *realloc();
  2252.  
  2253.   ret=realloc(ptr,size);
  2254.   if(ret==(void *)0)
  2255.     fatal ("Virtual memory exhausted");
  2256.   return ret;
  2257. }
  2258.  
  2259. /* Implement an expandable fifo buffer of pointers
  2260.    (we don't care what they point to)
  2261.    which is used for conducting breadth-first tree walks in the call graph.
  2262.  
  2263.    The fifo is implemented as a ring buffer.  */
  2264.  
  2265. /* Vector of space allocated for the ring buffer elements.  */
  2266. static void **ring_buffer;
  2267.  
  2268. /* Number of elements allocated in the vector.  */
  2269. static int size_of_ring_buffer;
  2270.  
  2271. /* Pointer to next ring-buffer element to push into.
  2272.    After pushing the last element, this cycles to the first element.  */
  2273. static void **push_to_here;
  2274.  
  2275. /* Pointer to next ring-buffer element to pop from.
  2276.    After pushing the last element, this cycles to the first element.  */
  2277. static void **pop_from_here;
  2278.  
  2279. #define BUG(x,msg,val)
  2280. #define BUG2(x,msg,v1,v2)
  2281. #define BUG3(x,msg,v1,v2,v3)
  2282. #define BUGX(x,msg)
  2283.  
  2284. /* Allocate space for the ring buffer and mark it empty.  */
  2285.  
  2286. void
  2287. init_ring_buffer(void)
  2288. {
  2289.   size_of_ring_buffer=40;
  2290.   ring_buffer=(void **)ck_calloc(size_of_ring_buffer,sizeof(void **));
  2291.   push_to_here=ring_buffer;
  2292.   pop_from_here=ring_buffer;
  2293. }
  2294.  
  2295. /* Add the value N at the end of the ring buffer.  */
  2296.  
  2297. void
  2298. push_ring_buffer(void *n)
  2299. {
  2300.   if(pop_from_here+1==push_to_here) { /* Ring buffer is full? */
  2301.     int f,t;
  2302.  
  2303.     BUGX(DB_RB,("Expanding ring buffer by %d",size_of_ring_buffer));
  2304.     f=pop_from_here-ring_buffer;
  2305.     t=push_to_here-ring_buffer;
  2306.     size_of_ring_buffer*=2;
  2307.     ring_buffer=ck_realloc((void *)ring_buffer,size_of_ring_buffer*sizeof(void **));
  2308.     pop_from_here=ring_buffer+f;
  2309.     push_to_here=ring_buffer+t;
  2310.   }
  2311.   BUGX(DB_RB,("pushing %x on ring buffer",n));
  2312.   *push_to_here=n;
  2313.   push_to_here++;
  2314.   if(push_to_here==ring_buffer+size_of_ring_buffer)
  2315.     push_to_here=ring_buffer;
  2316. }
  2317.  
  2318. /* Remove and return the value at the head of the ring buffer.  */
  2319.  
  2320. void *
  2321. pop_ring_buffer(void)
  2322. {
  2323.   void *ret;
  2324.  
  2325.   if(pop_from_here==push_to_here)
  2326.     abort();
  2327.  
  2328.   ret= *pop_from_here;
  2329.   pop_from_here++;
  2330.   if(pop_from_here==ring_buffer+size_of_ring_buffer)
  2331.     pop_from_here=ring_buffer;
  2332.   BUGX(DB_RB,("popping %x from ring buffer",ret));
  2333.   return ret;
  2334. }
  2335.  
  2336. /* 1 if there is any data in the ring buffer.  */
  2337.  
  2338. int
  2339. ring_buffer_isnt_empty(void)
  2340. {
  2341.   BUGX(DB_RB,("Ring buffer %s empty",pop_from_here==push_to_here?"IS":"is not"));
  2342.   return pop_from_here!=push_to_here;
  2343. }
  2344.  
  2345. /* Functions to print debugging messages.  */
  2346.  
  2347. /* Like vprintf but print an extra newline at the end.  */
  2348.  
  2349. void
  2350. dbgprintf(char *s,...)
  2351. {
  2352.   va_list iggy;
  2353.  
  2354.   va_start(iggy,s);
  2355.   vfprintf(stderr,s,iggy);
  2356.   putc('\n',stderr);
  2357.   va_end(iggy);
  2358. }
  2359.  
  2360. /* Print out the entire symbol table */
  2361.  
  2362. void
  2363. dumpsyms(void)
  2364. {
  2365.   struct mesym *np,*endp;
  2366.   struct symlist *sy;
  2367.   int n;
  2368.   char buf[80];
  2369.  
  2370.   n=0;
  2371.   for(np=syms,endp= &syms[nsym];np<endp;np++) {
  2372.     sprintf(buf,"%-15s %#6lx %d %2d.%2d[%d]",
  2373.         np->name, np->value,np->histo,np->ncalled,np->ncalls,np->cycnum);
  2374.     if(n==0) {
  2375.       printf("%-35s",buf);
  2376.       n++;
  2377.     } else {
  2378.       printf("%s\n",buf);
  2379.       n=0;
  2380.     }
  2381.   }
  2382.   putchar('\n');
  2383.   if(n==0) putchar('\n');
  2384. }
  2385.  
  2386. /* Print out the call tree vector */
  2387. void
  2388. dumpfuns(void)
  2389. {
  2390.   struct mesym *np;
  2391.   struct symlist *sy;
  2392.   int    n;
  2393.  
  2394.   for(n=0;n<number_of_functions_in_call_tree;n++) {
  2395.     np=functions_in_call_tree[n];
  2396.     printf("%s{%d}[%d]  ",np->name,np->cycnum,np->ncalls);
  2397.     /* for(sy=np->called;sy;sy=sy->next)
  2398.        printf("%s{%d}(%d) ",sy->sym->name,sy->sym->cycnum,sy->ncalls);
  2399.        putchar('\n'); */
  2400.   }
  2401. }
  2402.